package com.example.deertrip.ui.society;

import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;

import com.example.deertrip.Hotel_Model_Activity;
import com.example.deertrip.Moment_Detail_Activity;
import com.example.deertrip.PostCommunityActivity;
import com.example.deertrip.R;
import com.example.deertrip.bean.Moment;
import com.example.deertrip.database.DBOperate;
import com.example.deertrip.databinding.FragmentSocietyBinding;
import com.example.deertrip.tools_class.DrawableHelp;
import com.example.deertrip.tools_class.UserStateGet;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SocietyFragment extends Fragment {
    private List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
    private ListView listView;
    private SimpleAdapter simpleAdapter = null;

    private DBOperate dbOperate;
    private FragmentSocietyBinding binding;
    private Button btn;
    private UserStateGet userStateGet=new UserStateGet();

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        SocietyViewModel societyViewModel =
                new ViewModelProvider(this).get(SocietyViewModel.class);
        userStateGet.vlidate(getActivity());
        binding = FragmentSocietyBinding.inflate(inflater, container, false);
        View root = binding.getRoot();
        listView=root.findViewById(R.id.society_lv);
        btn=root.findViewById(R.id.Btn_PostNews);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(getActivity(), PostCommunityActivity.class);
                startActivity(intent);
            }
        });


        dbOperate=new DBOperate(this.getContext());
        sql();
        return root;
    }

     public void sql(){
         List<Moment> moments= dbOperate.get_All_Moment_Data();
         List<HashMap<String, Object>> data= new ArrayList<HashMap<String, Object>>();//存放全部数据的列表
         Drawable dw;

         for(int i=0;i<moments.size();i++){
             HashMap<String, Object> items=new HashMap<String, Object>(); //存放一条数据的哈希表
             Moment moment=moments.get(i);
             BitmapDrawable bd=new BitmapDrawable(this.getResources(),moment.getImage());//将Test对象中读取的点阵图片转换为bitmapdrawable
             dw=bd;    //再转换为Drawable
             items.put("image", dw);   //属性分别存入哈希表
             items.put("content", moment.getContent());
             items.put("username", moment.getUsername());
             items.put("good", moment.getGood());
             data.add(items);   //总数据哈希表中加入该条数据项
         }


         SimpleAdapter adapter =null;
         adapter= new SimpleAdapter(this.getContext(), data, R.layout.moment,
                 new String[]{"image","content", "username","good"}, new int[]{R.id.moment_image,R.id.moment_content, R.id.moment_name, R.id.moment_good})
         {
             @Override
             public View getView(int position,View convertView,ViewGroup parent){
                 final int p=position;
                 Button goodbtn;
                 TextView goodtv;
                 convertView = super.getView(position, convertView, parent);
                 if(convertView==null) {
                     convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.moment, null);
                 }
                 goodtv = convertView.findViewById(R.id.moment_good);
                 goodbtn = convertView.findViewById(R.id.moment_good_button);


                 goodbtn.setOnClickListener(new View.OnClickListener()
                 {

                     @Override
                     public void onClick(View v )
                     {
                         HashMap<String, Object> items=new HashMap<String, Object>();
                         items=(HashMap<String, Object>) getItem(p);
                         Integer id= (Integer) items.get("id");
                         dbOperate.GoodAdd(id);
                         Integer good=(Integer) items.get("good")+1;
                         items.put("good",good);
                         data.add(p,items);
                         String str=good.toString();
                         goodtv.setText(str);
                     }
                 });
                 return convertView;
             }
         };

         adapter.setViewBinder(new SimpleAdapter.ViewBinder(){
             public boolean setViewValue(View view,Object data,String textRepresentation){
                 if(view instanceof ImageView && data instanceof Drawable){
                     ImageView iv=(ImageView)view;
                     iv.setImageDrawable((Drawable)data);
                     return true;
                 }
                 else return false;
             }
         });

         listView.setAdapter(adapter);
         listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
             @Override
             public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                 HashMap<String, Object> items=new HashMap<String, Object>();
                 Bundle bundle=new Bundle();
                 items=data.get(i);
                 bundle.putString("username",(String) items.get("username"));
                 bundle.putString("content",(String)items.get("content"));
                 bundle.putInt("good",(int)items.get("good"));

                 Drawable image=(Drawable) items.get("image");
                 byte[] out= DrawableHelp.drawableTobyte(image);

                 Intent intent=new Intent(getActivity(), Moment_Detail_Activity.class);
                 intent.putExtras(bundle);
                 intent.putExtra("image",out);
                 startActivity(intent);
             }
         });

     }
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }
}